using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Pitoshka.Tools.ResourceFilesChecker
{
	internal class ResourceFile
	{
		private string _DefaultFileName;
		public string DefaultFileName
		{
			get { return _DefaultFileName; }
			set { _DefaultFileName = value; }
		}

		private List<String> _OtherCulturesFiles = new List<string>();
		public List<String> OtherCulturesFiles
		{
			get { return _OtherCulturesFiles; }
			set { _OtherCulturesFiles = value; }
		}

		private Dictionary<string, List<KeyValuePair<EnumErrorType, string>>> _Errors = new Dictionary<string, List<KeyValuePair<EnumErrorType, string>>>();
		public Dictionary<string, List<KeyValuePair<EnumErrorType, string>>> Errors
		{
			get { return _Errors; }
			set { _Errors = value; }
		}

		private List<string> _Keys = new List<string>();
		public List<string> Keys
		{
			get { return _Keys; }
			set { _Keys = value; }
		}

		private XmlDocument _DefaultLocFile = new XmlDocument();
		public XmlDocument DefaultLocFile
		{
			get { return _DefaultLocFile; }
		}

		public ResourceFile(string file)
		{
			this.DefaultFileName = file;

			_DefaultLocFile.Load(DefaultFileName);
			XmlNodeList values = DefaultLocFile.SelectNodes("/root/data");
			foreach (XmlNode node in values)
			{
				this.Keys.Add(node.Attributes["name"].Value);
			}
		}

		public void Check()
		{
			this.Errors.Clear();
			this.OtherCulturesFiles.Clear();

			foreach (string cul in Program.Cultures)
			{
				foreach (string ext in Program.AllowedExtensions)
				{
					string otherCultureFile = DefaultFileName.Replace(
						"." + ext + ".resx"
						, "." + ext + "." + cul + ".resx"
					);

					if (File.Exists(otherCultureFile))
					{
						this.OtherCulturesFiles.Add(otherCultureFile);
					}
					else
					{
						this.Errors.Add(otherCultureFile, new List<KeyValuePair<EnumErrorType, string>>());
						this.Errors[otherCultureFile].Add(
							new KeyValuePair<EnumErrorType, string>(EnumErrorType.FileNotExist, "File not exists")
						);
					}
				}
			}

			foreach (string file in this.OtherCulturesFiles)
			{
				XmlDocument xDoc = new XmlDocument();
				xDoc.Load(file);

				this.Errors.Add(file, new List<KeyValuePair<EnumErrorType, string>>());
				foreach (string key in this.Keys)
				{
					if (xDoc.SelectSingleNode("/root/data[@name='" + key + "']") == null)
					{
						this.Errors[file].Add(
							new KeyValuePair<EnumErrorType, string>(EnumErrorType.NotEnoughtKey, key)
						);
					}
				}
			}
		}
		public void FixErrors()
		{
			foreach (KeyValuePair<string, List<KeyValuePair<EnumErrorType, string>>> errorFile in this.Errors)
			{
				XmlDocument locDocument = null;
				foreach (KeyValuePair<EnumErrorType, string> error in errorFile.Value)
				{

					switch (error.Key)
					{
						case EnumErrorType.FileNotExist:
							File.Copy(this.DefaultFileName, errorFile.Key);
							break;
						case EnumErrorType.NotEnoughtKey:
							if (locDocument == null)
							{
								locDocument = new XmlDocument();
								locDocument.Load(errorFile.Key);
							}

							XmlNode importedNode = locDocument.ImportNode(
								DefaultLocFile.SelectSingleNode("/root/data[@name='" + error.Value + "']")
								, true
							);

							locDocument.SelectSingleNode("/root").AppendChild(importedNode);
							break;
						default:
							break;
					}
				}

				if (locDocument != null)
				{
					locDocument.Save(errorFile.Key);
				}
			}
		}
	}
}
